// DlgEditILL.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditILL.h"
#include "DlgPickList.h"


// CDlgEditILL dialog

IMPLEMENT_DYNAMIC(CDlgEditILL, CDialog)

CDlgEditILL::CDlgEditILL(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditILL::IDD, pParent)
	, m_nID(-1)
	, m_bAdd(FALSE)
	, m_szID(_T(""))
	, m_szDescription(_T(""))
	, m_szExplanation(_T(""))
	, m_szMessageSubject(_T(""))
	, m_bUserListed(FALSE)
{
	m_nOldID = -1;
	m_nIndexID = -1;
}

CDlgEditILL::~CDlgEditILL()
{
}

void CDlgEditILL::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_ID, m_szID);
	DDX_Text(pDX, IDC_EDIT_DESCRIPTION, m_szDescription);
	DDX_Text(pDX, IDC_EDIT_EXPLANATION, m_szExplanation);
	DDX_Text(pDX, IDC_EDIT_STATUS_SUBJECT, m_szMessageSubject);
	DDX_Check(pDX, IDC_CHECK_USER_LISTED, m_bUserListed);
	DDX_Control(pDX, IDC_SPIN_RECORD, m_ctrlRecord);
}


BEGIN_MESSAGE_MAP(CDlgEditILL, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_APPLY, &CDlgEditILL::OnBnClickedButtonApply)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CDlgEditILL::OnBnClickedButtonDelete)
	ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_RECORD, &CDlgEditILL::OnDeltaposSpinRecord)
END_MESSAGE_MAP()


// CDlgEditILL message handlers


BOOL CDlgEditILL::OnInitDialog()
{
	CDialog::OnInitDialog();

	CStringArray szaILL;
	ILL_LOOKUP ILL;

	appConfig.m_SQL.GetDescriptions( CMySQLEx::ill, &szaILL, CMySQLEx::list, &m_naID );

	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );

	// check if adding
	if( m_bAdd )
	{
		return TRUE;
	}
	else
	{
		GetDlgItem( IDC_EDIT_ID )->EnableWindow( FALSE );
	}

	// pick list
	if( m_nID < 0 )
	{
		// show pick list
		CDlgPickList PickListDlg;
		PickListDlg.m_pszaStrings = &szaILL;
		if( PickListDlg.DoModal() == IDOK )
			m_nID = _tstol( PickListDlg.m_szString.Left( PickListDlg.m_szString.Find( _T(" ") )));
		else
		{
			EndDialog( IDCANCEL );
			return TRUE;
		}
	}

	// get ILL
	if( !appConfig.m_SQL.GetDescriptionILL( &ILL, m_nID ))
	{
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = ILL.szDescription;
	m_szExplanation = ILL.szExplanation;
	m_szMessageSubject = ILL.szMessageSubject;
	m_bUserListed = ILL.nUserListed;

	// set index
	for( INT nIndex = 0; nIndex < m_naID.GetCount(); nIndex++ )
		if( m_nID == m_naID.GetAt( nIndex ))
		{
			m_nIndexID = nIndex;
			m_ctrlRecord.SetPos( nIndex );
			break;
		}
	// no index found
	if( m_nIndexID < 0 )
		m_ctrlRecord.EnableWindow( FALSE );

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CDlgEditILL::OnOK()
{
	// save old id
	m_nOldID = m_nID;
	// update record
	UpdateRecord();

	CDialog::OnOK();
}


void CDlgEditILL::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}


void CDlgEditILL::OnBnClickedButtonApply()
{
	// save old id
	m_nOldID = m_nID;
	// update record
	UpdateRecord();
}


void CDlgEditILL::OnBnClickedButtonDelete()
{
	// update data
	UpdateData();

	// get ILL id
	m_nID = _tstol( m_szID );

	// check for 0
	if( m_nID == 0 )
	{
		CString szMessage;
		AfxFormatString1( szMessage, IDS_ERROR_DELETE_ITEM_0, _T("ILL") );
		AfxMessageBox( szMessage );
		return;
	}
	// check if sure
	if( AfxMessageBox( IDS_DELETE_ILL, MB_YESNO ) == IDNO )
		return;

	// remove row
	appConfig.m_SQL.RemoveRow( CMySQLEx::ill, m_nID );

	// adjust requests
	appConfig.m_SQL.UpdateFieldValue( CMySQLEx::requests, _T("ill_id"), m_nID, 0 );

	// remove from array
	m_naID.RemoveAt( m_nIndexID );
	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );
	// reset index
	m_nIndexID = 0;
	m_nID = m_naID.GetAt( m_nIndexID );

	// get ILL
	ILL_LOOKUP ILL;
	if( !appConfig.m_SQL.GetDescriptionILL( &ILL, m_nID ))
	{
		EndDialog( IDCANCEL );
		return;
	}

	// get variables
	m_szID.Format( _T("%ld"), m_nID );
	m_szDescription = ILL.szDescription;
	m_szExplanation = ILL.szExplanation;
	m_szMessageSubject = ILL.szMessageSubject;
	m_bUserListed = ILL.nUserListed;

	//update data
	UpdateData( FALSE );
	// reset old id
	m_nOldID = -1;
}


void CDlgEditILL::OnDeltaposSpinRecord(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);

	// increment index
	m_nIndexID += pNMUpDown->iDelta;
	if( m_nIndexID > m_naID.GetUpperBound() )
	{
		Beep( 750, 300 );
		m_nIndexID = m_naID.GetUpperBound();
		//m_nIndexID = 0;
		return;
	}
	if( m_nIndexID < 0 )
	{
		Beep( 750, 300 );
		m_nIndexID = 0;
		//m_nIndexID = (INT)m_naID.GetUpperBound();
		return;
	}

	// get ILL
	ILL_LOOKUP ILL;
	if( !appConfig.m_SQL.GetDescriptionILL( &ILL, m_naID.GetAt( m_nIndexID )))
	{
		EndDialog( IDCANCEL );
		return;
	}

	// get variables
	m_szID.Format( _T("%ld"), ILL.nID );
	m_szDescription = ILL.szDescription;
	m_szExplanation = ILL.szExplanation;
	m_szMessageSubject = ILL.szMessageSubject;
	m_bUserListed = ILL.nUserListed;

	// set ILL ID
	m_nID = _tstol( m_szID );

	//update data
	UpdateData( FALSE );

	*pResult = 0;
}


BOOL CDlgEditILL::UpdateRecord()
{
	// update data
	UpdateData();

	// check data
	if( m_szID.IsEmpty() || m_szDescription.IsEmpty() )
	{
		AfxMessageBox( _T("Data missing.\n\nPlease complete all data fields.") );
		return FALSE;
	}

	// get id
	m_nID = _tstol( m_szID );


	// check for overwrite
	if( m_bAdd )
	{
		if( appConfig.m_SQL.GetRowCount( CMySQLEx::ill, m_nID ) > 0 )
		{
			if( AfxMessageBox( IDS_ILL_CHANGE, MB_YESNO ) == IDNO )
			{
				// return
				return FALSE;
			}
		}
	}

	// check for update
	if( m_nOldID != -1 && m_nOldID != m_nID )
	{
		if( AfxMessageBox( IDS_UPDATE_ILL, MB_YESNO ) == IDYES )
		{
			// update requests
			appConfig.m_SQL.UpdateFieldValue( CMySQLEx::requests, _T("ill_id"), m_nOldID, m_nID );
		}		
	}

	CStringArray szaILL;
	ILL_LOOKUP ILL;

	// set variables
	ILL.nID = m_nID;
	ILL.szDescription = m_szDescription;
	ILL.szExplanation = m_szExplanation;
	ILL.szMessageSubject = m_szMessageSubject;
	ILL.nUserListed = m_bUserListed;


	// save it
	appConfig.m_SQL.SetDescriptionILL( &ILL, m_nID, m_bAdd );

	// fetch collection
	appConfig.m_SQL.GetDescriptions( CMySQLEx::ill, &szaILL, CMySQLEx::list, &m_naID );

	// set spin control
	m_ctrlRecord.SetRange( 0, m_naID.GetCount() - 1 );
	// set index
	for( INT nIndex = 0; nIndex < m_naID.GetCount(); nIndex++ )
		if( m_nID == m_naID.GetAt( nIndex ))
		{
			m_nIndexID = nIndex;
			m_ctrlRecord.SetPos( nIndex );
			break;
		}
	// no index found
	if( m_nIndexID < 0 )
		m_ctrlRecord.EnableWindow( FALSE );

	// update data
	UpdateData( FALSE );
	// reset old id
	m_nOldID = -1;

	return TRUE;
}
